package com.wpw.dream.sharding;

import java.util.Collection;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;

/**
 * 分库算法
 * 
 * Project Name：dream-mybatis-sharding-dao
 * ClassName：DbAlgorithm
 * Description：
 * @author: wupengwei
 * @date: 2018年4月4日 上午11:18:17
 * note:
 *
 */
public class DbAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Integer>  {
	
	@Override  
    public String doEqualSharding(Collection<String> collection, ShardingValue<Integer> shardingValue) {  

		//按照id分库
		int id = shardingValue.getValue();  
  
        int index = id % 2;  
  
        //可用的数据源的名称
        for (String each : collection) {  
            if (each.endsWith(index + "")) {  
                return each;  
            }  
        }  
        throw new UnsupportedOperationException();  
    }  
  
    @Override  
    public Collection<String> doInSharding(Collection<String> collection, ShardingValue<Integer> shardingValue) {  
        return null;  
    }  
  
    @Override  
    public Collection<String> doBetweenSharding(Collection<String> collection, ShardingValue<Integer> shardingValue) {  
        return null;  
    }  
}  
